package org.map.core
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import flash.system.Capabilities;
	import flash.system.TouchscreenType;
	
	
	public class IsoObject extends Sprite
	{
		
		protected var _position:Point3D;
		protected var _size:int;
		protected var _walkable:Boolean = false;
		protected var _vx:Number = 0;
		protected var _vy:Number = 0;
		protected var _vz:Number = 0;
		/**
		 * 对象数值 
		 */	    
		protected var _vo:IsoVO=new IsoVO()

		public function IsoObject()
		{
			this._position = new Point3D();
			this.updateScreenPosition();
			
		}
				
		/**
		 * 设置对象数值
		 * @param _vo
		 * 
		 */		
		public function set vo(_vo:IsoVO) : void
		{
			this._vo = _vo;
			
			position=vo.pos
			
		}
		
		public function get vo() : IsoVO
		{
			return this._vo;
		}
		
		/**
		 * 设置x,y,z方向上的速度
		 * @param _vx
		 * 
		 */		
		public function set vx(_vx:Number) : void
		{
			this._vx = _vx;

		}
		
		public function get vx() : Number
		{
			return this._vx;
		}
		
		public function set vy(_vy:Number) : void
		{
			this._vy = _vy;

		}
		
		public function get vy() : Number
		{
			return this._vy;
		}
		
		public function set vz(_vz:Number) : void
		{
			this._vz = _vz;

		}
		
		public function get vz() : Number
		{
			return this._vz;
		}
		/**
		 *刷新屏幕 
		 * 
		 */		
		
		private function updateScreenPosition() : void
		{
			var pos:Point2D = IsoUtil.isoToScreen(this._position);
			this.x = pos.x;
			this.y = pos.y;
			
			vo.pos=this._position
				
			var p:Point2D=IsoUtil.point3DToColRow(vo.pos)
				
			vo.col=p.x
			
			vo.row=p.y
		}
		/**
		 * 设置x,y,z坐标 
		 * @param _vx
		 * 
		 */		
		 public function set _x(value:Number) : void
		{
			this._position.x = value;
			this.updateScreenPosition();
		}
		
		 public  function get _x() : Number
		{
			return this._position.x;
		}
		
		 public  function set _y(value:Number) : void
		{
			this._position.y = value;
			this.updateScreenPosition();

		}
		
		 public function get _y() : Number
		{
			return this._position.y;
		}
		
		 public  function set _z(value:Number) : void
		{
			this._position.z = value;
			this.updateScreenPosition();
		}
		
		 public  function get _z() : Number
		{
			return this._position.z;
		}
		
		/**
		 * 设置位置 修改为2010-12-5
		 * @param pos 为长方菱形的位置数值
		 * 
		 */		
		public function set position(pos:Point3D) : void
		{
			this._position = pos
				
			vo.col=IsoUtil.point3DToColRow(pos).x
			vo.row=IsoUtil.point3DToColRow(pos).y
			vo.pos=pos
				
			this.updateScreenPosition();
		}
		
		public function get position() : Point3D
		{
			return this._position;
		}
		
		/**
		 * 设置是否为障碍 
		 * @param _walkable
		 * 
		 */		
		public function set walkable(_walkable:Boolean) : void
		{
			this._walkable = _walkable;
		}
		
		public function get walkable() : Boolean
		{
			return this._walkable;
		}
		/**
		 * 获取/设置尺寸 
		 * @return 
		 * 
		 */		
		public function get size() : int
		{
			return _size;
		}
		public function set size(_size:int) : void
		{
			 this._size=_size;
		}
		
		/**
		 * 获取深度 
		 * @return 
		 * 
		 */		
		public function get depth() : Number
		{
			return (this._position.x + this._position.z) * 0.866 - this._position.y * 0.707;
		}
		/**
		 * 获取占地面积 
		 * @return 
		 * 
		 */		
		public function get rect() : Rectangle
		{
			return new Rectangle(this.x - this.size / 2, this.z - this.size / 2, this.size, this.size);
		}
		
		override public function toString() : String
		{
			return "[IsoObject (x:" + this._position.x + ", y:" + this._position.y + ", z:" + this._position.z + ", cols:"+vo.cols+", rows:"+vo.rows+")]";
		}
		/**
		 * 克隆 
		 * @return 
		 * 
		 */		
		public function clone():IsoObject{
		
		   var obj:IsoObject=new IsoObject()
			   
		   obj.vo=vo.clone()

		   obj.position=  obj.vo.pos
			   
		   //obj.initView()
			
		   return obj
		
		}
		
		/**
		 * 检测鼠标点下的象素点是否为透明 
		 * @return 
		 * 
		 */	
		protected var bmd:BitmapData
		public function isNoPixelAlpha():Boolean{
			
			var rect:Rectangle=this.getBounds(this)
				
			var a:Number=this.transform.matrix.a
			var d:Number=this.transform.matrix.d
				
			var disx:Number = a>0?Math.round(rect.x*a):Math.round(rect.right*a)
			var disy:Number = d>0?Math.round(rect.y*d):Math.round(rect.bottom*d)
			
			if(bmd==null){
			
				bmd= new BitmapData(Math.max(1,Math.ceil(this.width)), Math.max(1,Math.ceil(this.height)), true, 0x00);
				
				var matrix:Matrix=new Matrix(a, 0, 0, d, -disx, -disy)	
				
			    bmd.draw(this,matrix)
					
			}
			
			//如果点击区域为透明像素则返回
			var color:Number=bmd.getPixel(this.mouseX-rect.x,this.mouseY-rect.y)

			return color!=0 
				
		}
		
		
	}
}
